/**
 * 
 */
package com.desitek.thunder.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.desitek.thunder.entity.AppUser;
import com.desitek.thunder.entity.UserGroup;
import com.desitek.thunder.repository.IGroupRepository;
import com.desitek.thunder.repository.IUserRepository;

/**
 * @author dt
 * 
 */
@Service
@Transactional
public class UserService implements IUserService {

	private IUserRepository<AppUser> userRepository;
	private IGroupRepository<UserGroup> groupRepository;

	public UserService() {
	}

	@Autowired
	public UserService(IUserRepository<AppUser> userRepository,
			IGroupRepository<UserGroup> groupRepository) {

		this.userRepository = userRepository;
		this.groupRepository = groupRepository;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.desitek.thunder.service.IUserService#createUser(com.desitek.thunder
	 * .entity.AppUser)
	 */

	@Override
	public void createUser(AppUser user) throws UserNotFoundException {

		// check if user exist
		if (userRepository.isUserActivated(user)) {

			throw new UserExistException("User Name " + user.getUsername()
					+ " already exist");
		}
		// create user/
		userRepository.persist(user);
		// assign default group VALIDATION_GROUP

//		UserGroup group = groupRepository.findByGroupName("");
//
//		UserGroupList grpList = new UserGroupList();
//		grpList.setAppUser(user);
//		grpList.setUserGroup(group);

		// generate activation key
		// send activation email

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.desitek.thunder.service.IUserService#assignGroup(com.desitek.thunder
	 * .entity.AppUser, com.desitek.thunder.entity.UserGroup)
	 */
	@Override
	public void assignGroup(AppUser user, UserGroup group) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.desitek.thunder.service.IUserService#activateAccount(com.desitek.
	 * thunder.entity.AppUser)
	 */
	@Override
	public void activateAccount(AppUser user) {
	userRepository.activateUser(user);

	}
}
